package uia.alumni.data;

import java.sql.Date;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;



/**
 *
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class Populator {
    /** The constructor creates the tables */
    public Populator() {
        Properties props = new Properties();
        props.put("toplink.ddl-generation", "drop-and-create-tables");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Alumni", props);

        //create the database tables
        emf.createEntityManager().close();
        emf.close();
    }


    /** The populate() method populates the database. */
    public void populate() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Alumni");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        
        User even = new User("even@uia.no", "even");
        even.addRole(Role.admin);
        even.addRole(Role.member);
        even.setAddress("Kristiansand");
        even.setDescription("Is the Java Teacher.");
        even.setFullName("Even Aaby Larsen");
        even.setPhone("1234 5678");
        even.addCvEntry(new CvEntry("Primary school",
                "Learned a lot",
                new Date(70, 8, 20),
                new Date(76, 6, 20)));
        even.addCvEntry(new CvEntry("Secondary school",
                "Learned more",
                new Date(76, 8, 20),
                new Date(79, 6, 20)));
        even.addCvEntry(new CvEntry("High school",
                "Did _you_ learn anything?",
                new Date(79, 8, 20),
                new Date(82, 6, 20)));
        even.addCvEntry(new CvEntry("University",
                "Learned everything",
                new Date(83, 8, 20),
                new Date(88, 12, 20)));
        even.addCvEntry(new CvEntry("Some experience",
                "Wow!",
                new Date(89, 1, 20),
                new Date(94, 6, 20)));
        even.addCvEntry(new CvEntry("More experience",
                "Learned a lot",
                new Date(94, 8, 20),
                new Date(System.currentTimeMillis())));
        em.persist(even);

        // users.
        User adm1 = new User("admin@uia.no", "admin");
        adm1.addRole(Role.admin);
        adm1.addRole(Role.member);
        adm1.addRole(Role.organization);
        adm1.setFullName("Admin");
        em.persist(adm1);

        User prof1 = new User("student@uia.no", "student");
        prof1.addRole(Role.member);
        prof1.setFullName("Student");
        em.persist(prof1);


        User uia = new User("uia@uia.no", "uia");
        uia.addRole(Role.organization);
        uia.addRole(Role.member);
        uia.setFullName("University Of Agder");
        em.persist(uia);

        User simon = new User("simon@uia.no", "simon");
        simon.addRole(Role.admin);
        simon.addRole(Role.member);
        simon.addRole(Role.organization);
        simon.setFullName("Simon Zimmermann");
        em.persist(simon);

        // Groups
        Group gr1 = new Group("It og Informasjonssystemer 2009", simon);
        gr1.setInfo("It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). ");
        em.persist(gr1);

        // Groups
        Group gr2 = new Group("It og Informasjonssystemer 2010", simon);
        gr2.setInfo("Information about the Group. yes oooohooo !. ");
        em.persist(gr2);

        // Add user to group
        GroupParticipation gp = new GroupParticipation(simon, gr1, Role.admin);
        em.persist(gp);
        simon.addGroup(gp);
        gr1.addMember(gp);

        GroupParticipation gp2 = new GroupParticipation(even, gr2, Role.member);
        em.persist(gp2);
        even.addGroup(gp2);
        gr2.addMember(gp2);

        GroupParticipation gp3 = new GroupParticipation(simon, gr2, Role.member);
        em.persist(gp3);
        simon.addGroup(gp3);
        gr2.addMember(gp3);

        
        em.getTransaction().commit();
        em.close();
        emf.close();
    }


    public static void main(String[] args) throws Exception {
        System.getProperties().list(System.out);

        try {
            Populator m = new Populator();
            m.populate();
        }
        catch (Exception e) {
            e.printStackTrace(System.out);
            throw e;
        }
    }

}
